package model;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectIO {
	public static void main(String[] args) {
		//ghi mot objec ra file
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("abc.dat"));
			
			Student s = new Student();
			s.setId(10);
			s.setName("xyz");
			
			oos.flush();
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//doc object tu file
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream("abc.dat"));
			
			Object obj = null;
				
				while((obj = ois.readObject()) != null) {
					
					if(obj instanceof Student) {
						int id = ((Student) obj).getId();
						System.out.print("ID: " + id);
						
						String name = ((Student) obj).getName();
						System.out.print("NAME: " + name);
					}
				}
		}catch (EOFException e) {
			// TODO: handle exception
		}catch (ClassNotFoundException e) {
			// TODO: handle exception
		} catch (FileNotFoundException e) {
			// TODO: handle exception
		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
